package com.tjian.fafa.user.base.friendRequest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.ConnectedApp;
import com.tjian.fafa.user.base.bean.FrdRequestEntity;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.base.chatServices.FriendListInfo;
import com.tjian.fafa.user.base.commons.GlobalMsgTypes;
import com.tjian.fafa.user.base.commons.GlobalStrings;
import com.tjian.fafa.user.base.mainBody.MainBodyActivity;
import com.tjian.fafa.user.base.myNetwork.NetworkService;

import java.util.ArrayList;

public class FriendSearchResultActivity extends Activity
{

    private static FriendSearchResultActivity mInstance;

    private ListView mListviewOfResults;

    private ArrayList<UserInfo> mListU;
    private UserInfo requestee;

    public static FriendSearchResultActivity getInstance()
    {
        return mInstance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cc0_friend_search_result);
        Log.i("friend", "begin");
        ConnectedApp.getInstance().addItemIntoListActivity(this);

        mInstance = this;

        mListviewOfResults = (ListView) findViewById(R.id.cc0_friend_search_result_listview);

        Intent intent0 = getIntent();
        String searchedResult = intent0.getStringExtra("searchResult");
        String[] strArr0 = searchedResult.split(GlobalStrings.searchListDivider);
        int num = Integer.parseInt(strArr0[0]);

        mListU = new ArrayList<UserInfo>();
        for (int p = 1; p <= num; p++)
        {
            UserInfo uux = new UserInfo(strArr0[p]);
            mListU.add(uux);
        }
        mListviewOfResults.setAdapter(new FriendSearchResultAdapter(this, mListU));
        mListviewOfResults.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                UserInfo mySelf = ConnectedApp.getInstance().getUserInfo();
                Log.i("bbbbbbbb:", "position=" + position);
                requestee = mListU.get(position);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        FriendSearchResultActivity.this);
                alertDialogBuilder.setTitle(null);
                if (mySelf == null)
                {
                    MainBodyActivity.getInstance().gotoMyLoginPageActivity();
                    return;
                }
                if (mySelf.getId() == requestee.getId())
                {
                    alertDialogBuilder
                            .setMessage("抱歉，你不能向自己发送好友申请")
                            .setCancelable(true)
                            .setPositiveButton("Ok", new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int id)
                                {
                                    dialog.cancel();
                                }
                            });
                }
                else if (!FriendListInfo.getFriendListInfo().isIdFriend(requestee.getId()))
                {
                    alertDialogBuilder
                            //					.setMessage("are you sure to send friend request?")
                            .setMessage("确定要发送好友请求吗?")
                            .setCancelable(true)
                            .setPositiveButton("是", new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int id)
                                {
                                    UserInfo myUser = ConnectedApp.getInstance().getUserInfo();
                                    FrdRequestEntity reqEnt0 = new FrdRequestEntity(myUser, requestee);
                                    reqEnt0.accept();
                                    NetworkService.getInstance().sendUpload(GlobalMsgTypes.msgFriendshipRequest, reqEnt0.toString());
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int id)
                                {
                                    dialog.cancel();
                                }
                            });
                }
                else
                {
                    alertDialogBuilder
                            .setMessage("已是好友!")
                            .setCancelable(true)
                            .setPositiveButton("Ok", new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int id)
                                {
                                    dialog.cancel();
                                }
                            });
                }

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mInstance = null;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        ConnectedApp.getInstance().setCurActivity(this);
    }
}
